package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by mgehwolf on 30/07/2014.
 */
public class LibraryItemList {

    private ArrayList<LibraryItem> libraryItems;

    public LibraryItemList(){
        libraryItems  = new ArrayList<LibraryItem>();
    }

    public void show(boolean availability, String Item) {
        if(availability) {
            System.out.println("The following " + Item + "s are available");
            for (int i = 0; i < libraryItems.size(); i++) {
                if (libraryItems.get(i).getAvailability() && libraryItems.get(i).getClass().getSimpleName().equals(Item)) {
                    System.out.println(libraryItems.get(i).getCode() + ") " + libraryItems.get(i).getName() + " " + libraryItems.get(i).getAuthor() + " " + libraryItems.get(i).getYear());
                }
            }
        } else{
            System.out.println("The following " + Item + "s are to be returned");
            for (int i = 0; i < libraryItems.size(); i++) {
                if (libraryItems.get(i).getAvailability()==false && libraryItems.get(i).getClass().getSimpleName().equals(Item)) {
                    System.out.println(libraryItems.get(i).getCode() + ") " + libraryItems.get(i).getName() + " " + libraryItems.get(i).getAuthor() + " " + libraryItems.get(i).getYear());
                }
            }
        }
    }

    public void addItem(int code, String name, String creator, String year, boolean availability, int rating) {
        libraryItems.add(new Movie(code, name, creator, year, availability, rating));
    }

    public void addItem(int code, String name, String creator, String year, boolean availability) {
        libraryItems.add(new Book(code, name, creator, year, availability));
    }

    public void checkOutItem(int code) {
        int count = libraryItems.size();
        for (int i=0; i< libraryItems.size();i++) {
            int Code = libraryItems.get(i).getCode();
            if (code == Code && libraryItems.get(i).getAvailability()) {
                System.out.println("successfully checked out "+ libraryItems.get(i).getClass().getSimpleName() + " " + libraryItems.get(i).getCode() + ") " + libraryItems.get(i).getName());
                libraryItems.get(i).setAvailability(false);
            } else count = count - 1;
        }
        if (count == 0) {
            System.out.println("Book is not available or does not exist");
        }

    }

    public void returnBook(int code) {
        int count = libraryItems.size();
        for (int i=0; i< libraryItems.size();i++) {
            int BookCode = libraryItems.get(i).getCode();
            if (code == BookCode && libraryItems.get(i).getAvailability()) {
                System.out.println("Successfully returned Book " + libraryItems.get(i).getCode() + ") " + libraryItems.get(i).getName());
                libraryItems.get(i).setAvailability(true);
            } else count = count - 1;
        }
        if (count == 0) {
            System.out.println("Book could not be returned");
        }

    }
}
